import _ from 'lodash';
import dynamic from 'next/dynamic';
import { useRouter } from 'next/router';
import Seo from '@/core/components/Seo';
import { getIdFromSlug, getNameFromSlug } from '@/core/utils/slug';
import Breadcrumb from '../../../lib/lob/components/Breadcrumb';
import { useEffect, useState } from 'react';
import odooApi from '@/core/api/odooApi';
import { div } from 'lodash-contrib';

const BasicLayout = dynamic(() => import('@/core/components/layouts/BasicLayout'));
const ProductSearch = dynamic(() => import('@/lib/product/components/ProductSearch'));
const CategorySection = dynamic(() => import('@/lib/product/components/CategorySection'));

export default function CategoryDetail() {
  const router = useRouter();
  const { slug = '', page = 1 } = router.query;
  const [dataLob, setDataLob] = useState([]);
  const [finalQuery, setFinalQuery] = useState({});
  const [dataCategoriesProduct, setDataCategoriesProduct] = useState([])
  const [data, setData] = useState([])
  const dataIdCategories = []
  
  const categoryName = getNameFromSlug(slug);
  const lobId = getIdFromSlug(slug);
  const q = router?.query.q || null;

  useEffect(() => {
    if(router.pathname.includes('lob')){
      const loadProduct = async () => {
        const lobData = await odooApi('GET', `/api/v1/lob_homepage/${lobId}/category_id`);
         
        if (lobData) {
          setDataLob(lobData);
        }
      };
      loadProduct();
      
    }
  }, [lobId]);

  useEffect(() => {
     if (router.pathname.includes('lob')){
      
      const fetchCategoryData = async () => {
        if (dataLob[0]?.categoryIds) {
          
          for (const cate of dataLob[0].categoryIds) {
           
            dataIdCategories.push(cate.childId)
          }
          
        
          setData(dataIdCategories.flat())
         
          // const newQuery = {
          //   fq: `category_id_ids:(${mergedArray.join(' OR ')})`,
          //   category : router.query.category? router.query.category : '',
          //   page : router.query.page? router.query.page : 1,
          //   brand : router.query.brand? router.query.brand : '',
          //   priceFrom,
          //   priceTo,
          //   limit,
          //   orderBy
          // };
       
          // setFinalQuery(newQuery);
         
        }
      };
      fetchCategoryData();
    }
  }, [dataCategoriesProduct, dataLob]);
  const query = {
    fq: `category_id_ids:(${data.join(' OR ')})`,
    page,
  };
  if (q) {
    query.q = q;
  }

  return (
    <BasicLayout>
      <Seo
        title={`Beli ${categoryName} di Indoteknik`}
        description={`Jual ${categoryName} Kirim Jakarta Surabaya Semarang Makassar Manado Denpasar Balikpapan Medan Palembang Lampung Bali Bandung Makassar Manado.`}
        additionalMetaTags={[
          {
            property: 'keywords',
            content: `Jual ${categoryName}, harga ${categoryName}, ${categoryName} murah, toko ${categoryName}, ${categoryName} jakarta, ${categoryName} surabaya`,
          },
        ]}
      />

      <Breadcrumb categoryId={getIdFromSlug(slug)} />
      <div className='grid grid-cols-3'>
        {dataLob[0]?.categoryIds && (
          <div>
            <image></image>
          </div>
        )}
      </div>

      {!_.isEmpty(router.query) && (
        <ProductSearch query={query} categories={getIdFromSlug(slug)} prefixUrl={`/shop/lob/${slug}`} />
      )}
    </BasicLayout>
  );
}
